package mysql

import (
	"gorm.io/gorm"
	"yukao1/models/global"
	"yukao1/models/request"
)

// 订单表
type OrderYk struct {
	gorm.Model
	OrderOn     string        `gorm:"type:varchar(255);comment:订单编号;"`
	UserId      int           `gorm:"type:int(10);comment:用户id;"`
	OrderPrice  float64       `gorm:"type:decimal(10,2);comment:订单价格;"`
	OrderStatus int           `gorm:"type:tinyint(1);comment:订单状态 0代付款 1待发货 2待收货 3已完成;"`
	DetailId    int           `gorm:"type:int(10);comment:订单详情id;"`
	Detail      OrderDetailYk `gorm:"foreignKey:DetailId"`
}

// 新增订单
func CreateOrder(data request.OrderRequest) OrderYk {
	var order OrderYk                                     //订单
	var goods GoodsYk                                     //商品
	global.DB.Where("id = ?", data.GoodsId).First(&goods) //新增前查询商品
	order = OrderYk{
		OrderOn:     data.OrderOn,
		UserId:      data.UserId,
		OrderPrice:  data.OrderPrice,
		OrderStatus: data.OrderStatus,
		DetailId:    int(order.ID),
		Detail: OrderDetailYk{
			GoodsId:    int(goods.ID),
			GoodsName:  goods.GoodsName,
			GoodsPrice: goods.GoodsPrice,
			Num:        data.Num,
		},
	}
	global.DB.Create(&order)
	return order

}

// 根据用户id查询订单
func GetUserIdOrder(data request.OrderRequest) []OrderYk {
	var order []OrderYk
	global.DB.Where("user_id", data.UserId).Find(&order)
	return order
}
